package com.example.myapplication.service;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.ConnectivityManager;
import android.os.Binder;
import android.os.Build;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.myapplication.listener.OnNetworkMusicPreparedListener;

import java.io.IOException;

public class MusicPlayService {
    private MediaPlayer mediaPlayer;

    public void playMusic(String url){
        releaseMediaPlayer();

        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(url);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    Log.e("MediaPlayer ","开始播放");
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void pause(){
        if (mediaPlayer.isPlaying()){
            mediaPlayer.pause();
            Log.e("pause: ", "暂停播放");
        }
    }

    public void start(){
        if (!mediaPlayer.isPlaying()){
            mediaPlayer.start();
            Log.e("start: ", "播放");
        }
    }

    public int getMusicDuration(){
        return mediaPlayer.getDuration();
    }

//    public int getCurPosition(){
//        return mediaPlayer.getCurrentPosition();
//    }

    public void releaseMediaPlayer(){
        if (mediaPlayer != null){
            mediaPlayer.release();

            mediaPlayer = null;
        }

    }
}
